package com.zoroki.hmusic.controller;


import com.zoroki.hmusic.constant.MessageConstant;
import com.zoroki.hmusic.entity.*;
import com.zoroki.hmusic.service.IFavoritePlaylistService;
import com.zoroki.hmusic.service.IFavoriteService;
import com.zoroki.hmusic.service.IFavoriteSongService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zoroki
 * @since 2023-05-10
 */
@RestController
@RequestMapping("/favorite")
@CrossOrigin("*")
@Tag(name = "收藏")
public class FavoriteController {

    @Autowired
    private IFavoriteService favoriteService;

    @Autowired
    private IFavoriteSongService favoriteSongService;

    @Autowired
    private IFavoritePlaylistService favoritePlaylistService;


    @Operation(summary = "传入favoriteSong来收藏歌曲")
    @PostMapping("/addSongToFavorite")
    @ResponseBody
    public Result addSongToFavorite(@RequestBody FavoriteSong favoriteSong) {
        int i = favoriteSongService.addSongToFavorite(favoriteSong);
        if (i > 0) {
            return new Result(true, MessageConstant.FAVORITE_SONG_ADD_SUCCESS, null);
        }
        return new Result(false, MessageConstant.FAVORITE_SONG_ADD_FAILURE, null);
    }


    @Operation(summary = "传入favoritePlaylist来收藏歌单")
    @PostMapping("/addPlaylistToFavorite")
    @ResponseBody
    public Result addPlaylistToFavorite(@RequestBody FavoritePlaylist favoritePlaylist) {
        int i = favoritePlaylistService.addPlaylistToFavorite(favoritePlaylist);
        if (i > 0) {
            return new Result(true, MessageConstant.FAVORITE_SONG_ADD_SUCCESS, null);
        }
        return new Result(false, MessageConstant.FAVORITE_SONG_ADD_FAILURE, null);
    }

    @Operation(summary = "检查歌曲是否为收藏歌曲")
    @GetMapping ("/checkSongIsFavorite")
    @ResponseBody
    public Result checkSongIsFavorite(@RequestParam int favoriteId, @RequestParam int songId) {
        FavoriteSong favoriteSong = favoriteSongService.checkSongIsFavorite(favoriteId, songId);
        if (favoriteSong != null) {
            return new Result(true, MessageConstant.FAVORITE_SONG_CHECK_SUCCESS, null);
        }
        return new Result(false, MessageConstant.FAVORITE_SONG_CHECK_FAILURE, null);
    }

    @Operation(summary = "检查歌单是否为收藏歌单")
    @GetMapping ("/checkPlaylistIsFavorite")
    @ResponseBody
    public Result checkPlaylistIsFavorite(@RequestParam int favoriteId, @RequestParam int playlistId) {
        FavoritePlaylist favoritePlaylist = favoritePlaylistService.checkPlaylistIsFavorite(favoriteId, playlistId);
        if (favoritePlaylist != null) {
            return new Result(true, MessageConstant.FAVORITE_PLAYLIST_CHECK_SUCCESS, null);
        }
        return new Result(false, MessageConstant.FAVORITE_PLAYLIST_CHECK_FAILURE, null);
    }


    @Operation(summary = "根据用户Id查询收藏歌曲")
    @GetMapping("/findFavoriteSongByUserId")
    @ResponseBody
    public Result findFavoriteSongByUserId(@RequestParam int userId) {
        List<Song> songs = favoriteService.findFavoriteSongByUserId(userId);
        if (songs.size() > 0) {
            return new Result(true, MessageConstant.YES, songs);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据用户Id查询收藏歌单")
    @GetMapping("/findFavoritePlaylistByUserId")
    @ResponseBody
    public Result findFavoritePlaylistByUserId(@RequestParam int userId) {
        List<Playlist> playlists = favoriteService.findFavoritePlaylistByUserId(userId);
        if (playlists.size() > 0) {
            return new Result(true, MessageConstant.YES, playlists);
        }
        return new Result(false, MessageConstant.NO, null);
    }

    @Operation(summary = "根据用户Id获取favorite")
    @GetMapping("/getFavoriteByUserId")
    @ResponseBody
    public Result getFavoriteByUserId(@RequestParam int userId) {
        Favorite favorite = favoriteService.getFavoriteByUserId(userId);
        if (favorite != null) {
            return new Result(true, MessageConstant.FAVORITE_GET_SUCCESS, favorite);
        }
        return new Result(false, MessageConstant.FAVORITE_GET_FAILURE, null);
    }

    @Operation(summary = "根据用户Id和歌曲Id从收藏中删除歌曲")
    @DeleteMapping("/deleteFavoriteSongByUserId")
    @ResponseBody
    public Result deleteFavoriteSongByUserId(@RequestParam int userId, @RequestParam int songId) {
        int i = favoriteSongService.deleteFavoriteSongByUserId(userId, songId);
        if (i > 0) {
            return new Result(true, MessageConstant.FAVORITE_SONG_DELETE_SUCCESS, null);
        }
        return new Result(false, MessageConstant.FAVORITE_SONG_DELETE_FAILURE, null);
    }

    @Operation(summary = "根据用户Id和歌单Id从收藏中删除歌曲")
    @DeleteMapping("/deleteFavoritePlaylistByUserId")
    @ResponseBody
    public Result deleteFavoritePlaylistByUserId(@RequestParam int userId, @RequestParam int playlistId) {
        int i = favoritePlaylistService.deleteFavoritePlaylistByUserId(userId, playlistId);
        if (i > 0) {
            return new Result(true, MessageConstant.FAVORITE_PLAYLIST_DELETE_SUCCESS, null);
        }
        return new Result(false, MessageConstant.FAVORITE_PLAYLIST_DELETE_FAILURE, null);
    }
}
